/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.itschapala.dsw2.vista;

import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import mx.itschapala.dsw2.biblioteca8a.BL.AutorBLLocal;
import mx.itschapala.dsw2.biblioteca8a.modelo.Autor;

/**
 *
 * @author Raven
 */
@ManagedBean
@SessionScoped
public class AutorBean {
    @EJB
    private AutorBLLocal autorBL;
    //------------------Variables--------------------
private List<Autor> lista;
private Autor author;
private String accion;
private int n;
     /**
     * Creates a new instance of AutorBean
     */
    public AutorBean() {
        author=new Autor();
    }
//----------------- Metodos GET y SET----------------------
    public List<Autor> getLista() {
        lista=autorBL.getAll();
        return lista;
        }
    
    
        public Autor getAuthor() {
        return author;
    }

    public void setAuthor(Autor author) {
        this.author = author;
    }
    
    
//-----------------Acciones------------------------------
    public String procesarAccion(){
        if(accion.equalsIgnoreCase("Nuevo")){
        autorBL.register(author);
        }
        else
        if(accion.equalsIgnoreCase("Editar")){
            autorBL.modify(author);
        }
        else if(accion.equalsIgnoreCase("Eliminar")){
            autorBL.delete(author);
        }
        accion="";
    return "AutorLista";
    }
    
    
   public String limpiarAccion(){
   author=new Autor();
   author.setNombre("eeeee");
   return "AutorCrearEditar";
    }
//-----------------Listener------------------------------

public void prepararNuevo(ActionEvent evt){
author=new Autor();
accion="Nuevo";
}
public void prepararEditar(ActionEvent evt)   
{
System.out.println(evt.getComponent().getClientId());
 n=Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
    System.out.println("el numero es:"+n);
    author=lista.get(n);
    accion="Editar";
}     


public void prepararEliminar(ActionEvent evt)   
{
System.out.println(evt.getComponent().getClientId());
 n=Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
    System.out.println("el numero es:"+n);
    author=lista.get(n);
    accion="Eliminar";
}    


}

